<?php 
$this->addTitle($lang['overview']['ally_title']);
echo "<h3>".$lang['overview']['ally_title']."</h3><br />";
echo "\n";

$this->sql->query("SELECT \n\t* \nFROM \n\t".$this->sql->getpre()."users");
$list_user="";
while($this->sql->fetch_assoc()){
	$list_user.="<option value=\"".$this->sql->row['user_id']."\">".$this->sql->row['user_name']."</option>\n";

};
$this->sql->query("SELECT \n\t* \nFROM \n\t`".$this->sql->getpre()."group`");
$list_group="";
while($this->sql->fetch_assoc()){
	$list_group.="<option value=\"".$this->sql->row['group_key']."\">".$this->sql->row['group_title']."</option>\n";

};

function list_array($array){
	$return="";
	if(is_array($array)){
		foreach($array as $value){
			if($return=="")
				$return="".$value;
			else
				$return.=", ".$value;
		}
	}
	return $return;
}

function options_list($options, $array){
	foreach($array as $value)
		$options=str_replace("value=\"".$value."\"", "value=\"".$value."\" selected=\"selected\"", $options);
		
	return $options;
}
$data_saved=false;
if(isset($_POST['planet_save'])){
	//print_r($_POST);
	
	$restrict_ally_groups='$restrict_ally_group=array(';
	$restrict_ally_users='$restrict_ally_user=array(';

	$restrict_user_groups='$restrict_user_group=array(';
	$restrict_user_users='$restrict_user_user=array(';
	
	$c=0;
	if(isset($_POST['restrict_ally_group'])){
		unset($restrict_ally_group);
		foreach($_POST['restrict_ally_group'] as $key=>$value){
			if($c==0){
				$c=1;
				$restrict_ally_groups.=$value;
			}else{
				$restrict_ally_groups.=", ".$value;
			}
			$restrict_ally_group[]=$value;
		}
	}
	
	$c=0;
	if(isset($_POST['restrict_ally_user'])){
		unset($restrict_ally_user);
		foreach($_POST['restrict_ally_user'] as $key=>$value){
			if($c==0){
				$c=1;
				$restrict_ally_users.=$value;
			}else{
				$restrict_ally_users.=", ".$value;
			}
			$restrict_ally_user[]=$value;
		}
	}
	
	$c=0;
	if(isset($_POST['restrict_user_group'])){
		unset($restrict_user_group);
		foreach($_POST['restrict_user_group'] as $key=>$value){
			if($c==0){
				$c=1;
				$restrict_user_groups.=$value;
			}else{
				$restrict_user_groups.=", ".$value;
			}
			$restrict_user_group[]=$value;
		}
	}
	
	$c=0;
	if(isset($_POST['restrict_user_user'])){
		unset($restrict_user_user);
		foreach($_POST['restrict_user_user'] as $key=>$value){
			if($c==0){
				$c=1;
				$restrict_user_users.=$value;
			}else{
				$restrict_user_users.=", ".$value;
			}
			$restrict_user_user[]=$value;
		}
	}
	
	$planet_datei = file("plugin/overview/planets.settings.php");
	$write='';
	foreach($planet_datei as $zeile) {
		//echo $zeile;
		$zeile=str_replace(" ", "", $zeile);
		$zeile=str_replace("\n", "", $zeile);
		$part=explode('=',$zeile);
		
		print_r($part[0]);
		
		switch($part[0]){
			case '$restrict_ally_group':
				$write.=$restrict_ally_groups.");\n";
				break;
			case '$restrict_ally_user':
				$write.=$restrict_ally_users.");\n";
				break;
			case '$restrict_ally_group':
				$write.=$restrict_ally_groups.");\n";
				break;
			case '$restrict_user_user':
				$write.=$restrict_user_users.");\n";
				break;
			default:
				$write.=$zeile."\n";
		}
	}
	
	$datei = fopen("plugin/overview/planets.settings.php","w");
	echo $write."</pre>";
	fwrite($datei,  stripslashes($write));
	fclose($datei);	

	
	$data_saved=true;
}

?>
<script language="javascript">
function change(){
	document.getElementById('data_changed').style.display="";
	document.getElementById('data_saved').style.display="none";
	document.getElementById('data_deleted').style.display="none";
}

</script>
<form id="planet" name="planet" method="post" action="">
<div align="center">
	<div id="data_changed" style="display:none; font-weight:bold; font-size:12px; color:#000000; width:50%; text-align:center; padding:4px; margin-bottom:8px; background:#E14517; border-style:solid; border-width:3px; border-color:#990000; filter:alpha(opacity=60); -moz-opacity: 0.6; opacity: 0.6;"><?php echo $lang['admin']['info_nsave']; ?></div>
	<div id="data_saved" style=" <?php if(isset($data_saved) && !$data_saved) { ?>display:none;<?php }; ?> font-weight:bold; font-size:12px; color:#000000; width:50%; text-align:center; padding:4px; margin-bottom:8px; background:#009900; border-style:solid; border-width:3px; border-color:#003300; filter:alpha(opacity=60); -moz-opacity: 0.6; opacity: 0.6;"><?php echo $lang['admin']['info_saved']; ?></div>
</div>
  <table border="0" cellspacing="0" cellpadding="3">
    <tr>
      <th height="48">Einschr&auml;nkung</th>
      <th>Gruppen</th>
      <th>Benutzer</th>
    </tr>
    <tr>
      <th>Allianz</th>
      <td><select name="restrict_ally_group[]" size="5" multiple="multiple" onchange="change()">
        <?php echo options_list($list_group,$restrict_ally_group); ?>
      </select></td>
      <td><select name="restrict_ally_user[]" size="5" multiple="multiple" onchange="change()">
        <?php echo options_list($list_user,$restrict_ally_user); ?>
      </select></td>
    </tr>
    <tr>
      <th>User</th>
      <td><select name="restrict_user_group[]" size="5" multiple="multiple" onchange="change()">
        <?php echo options_list($list_group,$restrict_user_group); ?>
      </select></td>
      <td><select name="restrict_user_user[]" size="5" multiple="multiple" onchange="change()">
        <?php echo options_list($list_user,$restrict_user_user); ?>
      </select></td>
    </tr>
  </table>
<input name="planet_save" type="submit" id="planet_save" value="Senden" />
</form>
